home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / jpegsrc4.zip / JDCOLOR.C < prev    next >
C/C++ Source or Header  |  1992-08-23  |  9KB  |  295 lines

  1. /*
  2.  * jdcolor.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains output colorspace conversion routines.
  9.  * These routines are invoked via the methods color_convert
  10.  * and colorout_init/term.
  11.  */
  12.  
  13. #include "jinclude.h"
  14.  
  15.  
  16. /**************** YCbCr -> RGB conversion: most common case **************/
  17.  
  18. /*
  19.  * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
  20.  * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
  21.  * The conversion equations to be implemented are therefore
  22.  *    R = Y                + 1.40200 * Cr
  23.  *    G = Y - 0.34414 * Cb - 0.71414 * Cr
  24.  *    B = Y + 1.77200 * Cb
  25.  * where Cb and Cr represent the incoming values less MAXJSAMPLE/2.
  26.  * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
  27.  *
  28.  * To avoid floating-point arithmetic, we represent the fractional constants
  29.  * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
  30.  * the products by 2^16, with appropriate rounding, to get the correct answer.
  31.  * Notice that Y, being an integral input, does not contribute any fraction
  32.  * so it need not participate in the rounding.
  33.  *
  34.  * For even more speed, we avoid doing any multiplications in the inner loop
  35.  * by precalculating the constants times Cb and Cr for all possible values.
  36.  * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
  37.  * for 12-bit samples it is still acceptable.  It's not very reasonable for
  38.  * 16-bit samples, but if you want lossless storage you shouldn't be changing
  39.  * colorspace anyway.
  40.  * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
  41.  * values for the G calculation are left scaled up, since we must add them
  42.  * together before rounding.
  43.  */
  44.  
  45. #ifdef SIXTEEN_BIT_SAMPLES
  46. #define SCALEBITS    14    /* avoid overflow */
  47. #else
  48. #define SCALEBITS    16    /* speedier right-shift on some machines */
  49. #endif
  50. #define ONE_HALF    ((INT32) 1 << (SCALEBITS-1))
  51. #define FIX(x)        ((INT32) ((x) * (1L<<SCALEBITS) + 0.5))
  52.  
  53. static int * Cr_r_tab;        /* => table for Cr to R conversion */
  54. static int * Cb_b_tab;        /* => table for Cb to B conversion */
  55. static INT32 * Cr_g_tab;    /* => table for Cr to G conversion */
  56. static INT32 * Cb_g_tab;    /* => table for Cb to G conversion */
  57.  
  58.  
  59. /*
  60.  * Initialize for colorspace conversion.
  61.  */
  62.  
  63. METHODDEF void
  64. ycc_rgb_init (decompress_info_ptr cinfo)
  65. {
  66.   INT32 i, x2;
  67.   SHIFT_TEMPS
  68.  
  69.   Cr_r_tab = (int *) (*cinfo->emethods->alloc_small)
  70.                 ((MAXJSAMPLE+1) * SIZEOF(int));
  71.   Cb_b_tab = (int *) (*cinfo->emethods->alloc_small)
  72.                 ((MAXJSAMPLE+1) * SIZEOF(int));
  73.   Cr_g_tab = (INT32 *) (*cinfo->emethods->alloc_small)
  74.                 ((MAXJSAMPLE+1) * SIZEOF(INT32));
  75.   Cb_g_tab = (INT32 *) (*cinfo->emethods->alloc_small)
  76.                 ((MAXJSAMPLE+1) * SIZEOF(INT32));
  77.  
  78.   for (i = 0; i <= MAXJSAMPLE; i++) {
  79.     /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
  80.     /* The Cb or Cr value we are thinking of is x = i - MAXJSAMPLE/2 */
  81.     x2 = 2*i - MAXJSAMPLE;    /* twice x */
  82.     /* Cr=>R value is nearest int to 1.40200 * x */
  83.     Cr_r_tab[i] = (int)
  84.             RIGHT_SHIFT(FIX(1.40200/2) * x2 + ONE_HALF, SCALEBITS);
  85.     /* Cb=>B value is nearest int to 1.77200 * x */
  86.     Cb_b_tab[i] = (int)
  87.             RIGHT_SHIFT(FIX(1.77200/2) * x2 + ONE_HALF, SCALEBITS);
  88.     /* Cr=>G value is scaled-up -0.71414 * x */
  89.     Cr_g_tab[i] = (- FIX(0.71414/2)) * x2;
  90.     /* Cb=>G value is scaled-up -0.34414 * x */
  91.     /* We also add in ONE_HALF so that need not do it in inner loop */
  92.     Cb_g_tab[i] = (- FIX(0.34414/2)) * x2 + ONE_HALF;
  93.   }
  94. }
  95.  
  96.  
  97. /*
  98.  * Convert some rows of samples to the output colorspace.
  99.  */
  100.  
  101. METHODDEF void
  102. ycc_rgb_convert (decompress_info_ptr cinfo, int num_rows, long num_cols,
  103.          JSAMPIMAGE input_data, JSAMPIMAGE output_data)
  104. {
  105. #ifdef SIXTEEN_BIT_SAMPLES
  106.   register INT32 y;
  107.   register UINT16 cb, cr;
  108. #else
  109.   register int y, cb, cr;
  110. #endif
  111.   register JSAMPROW inptr0, inptr1, inptr2;
  112.   register JSAMPROW outptr0, outptr1, outptr2;
  113.   register long col;
  114.   /* copy these pointers into registers if possible */
  115.   register JSAMPLE * range_limit = cinfo->sample_range_limit;
  116.   register int * Crrtab = Cr_r_tab;
  117.   register int * Cbbtab = Cb_b_tab;
  118.   register INT32 * Crgtab = Cr_g_tab;
  119.   register INT32 * Cbgtab = Cb_g_tab;
  120.   int row;
  121.   SHIFT_TEMPS
  122.   
  123.   for (row = 0; row < num_rows; row++) {
  124.     inptr0 = input_data[0][row];
  125.     inptr1 = input_data[1][row];
  126.     inptr2 = input_data[2][row];
  127.     outptr0 = output_data[0][row];
  128.     outptr1 = output_data[1][row];
  129.     outptr2 = output_data[2][row];
  130.     for (col = 0; col < num_cols; col++) {
  131.       y  = GETJSAMPLE(inptr0[col]);
  132.       cb = GETJSAMPLE(inptr1[col]);
  133.       cr = GETJSAMPLE(inptr2[col]);
  134.       /* Note: if the inputs were computed directly from RGB values,
  135.        * range-limiting would be unnecessary here; but due to possible
  136.        * noise in the DCT/IDCT phase, we do need to apply range limits.
  137.        */
  138.       outptr0[col] = range_limit[y + Crrtab[cr]];    /* red */
  139.       outptr1[col] = range_limit[y +            /* green */
  140.                  ((int) RIGHT_SHIFT(Cbgtab[cb] + Crgtab[cr],
  141.                             SCALEBITS))];
  142.       outptr2[col] = range_limit[y + Cbbtab[cb]];    /* blue */
  143.     }
  144.   }
  145. }
  146.  
  147.  
  148. /*
  149.  * Finish up at the end of the file.
  150.  */
  151.  
  152. METHODDEF void
  153. ycc_rgb_term (decompress_info_ptr cinfo)
  154. {
  155.   /* no work (we let free_all release the workspace) */
  156. }
  157.  
  158.  
  159. /**************** Cases other than YCbCr -> RGB **************/
  160.  
  161.  
  162. /*
  163.  * Initialize for colorspace conversion.
  164.  */
  165.  
  166. METHODDEF void
  167. null_init (decompress_info_ptr cinfo)
  168. /* colorout_init for cases where no setup is needed */
  169. {
  170.   /* no work needed */
  171. }
  172.  
  173.  
  174. /*
  175.  * Color conversion for no colorspace change: just copy the data.
  176.  */
  177.  
  178. METHODDEF void
  179. null_convert (decompress_info_ptr cinfo, int num_rows, long num_cols,
  180.           JSAMPIMAGE input_data, JSAMPIMAGE output_data)
  181. {
  182.   short ci;
  183.  
  184.   for (ci = 0; ci < cinfo->num_components; ci++) {
  185.     jcopy_sample_rows(input_data[ci], 0, output_data[ci], 0,
  186.               num_rows, num_cols);
  187.   }
  188. }
  189.  
  190.  
  191. /*
  192.  * Color conversion for grayscale: just copy the data.
  193.  * This also works for YCbCr/YIQ -> grayscale conversion, in which
  194.  * we just copy the Y (luminance) component and ignore chrominance.
  195.  */
  196.  
  197. METHODDEF void
  198. grayscale_convert (decompress_info_ptr cinfo, int num_rows, long num_cols,
  199.            JSAMPIMAGE input_data, JSAMPIMAGE output_data)
  200. {
  201.   jcopy_sample_rows(input_data[0], 0, output_data[0], 0,
  202.             num_rows, num_cols);
  203. }
  204.  
  205.  
  206. /*
  207.  * Finish up at the end of the file.
  208.  */
  209.  
  210. METHODDEF void
  211. null_term (decompress_info_ptr cinfo)
  212. /* colorout_term for cases where no teardown is needed */
  213. {
  214.   /* no work needed */
  215. }
  216.  
  217.  
  218.  
  219. /*
  220.  * The method selection routine for output colorspace conversion.
  221.  */
  222.  
  223. GLOBAL void
  224. jseldcolor (decompress_info_ptr cinfo)
  225. {
  226.   /* Make sure num_components agrees with jpeg_color_space */
  227.   switch (cinfo->jpeg_color_space) {
  228.   case CS_GRAYSCALE:
  229.     if (cinfo->num_components != 1)
  230.       ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
  231.     break;
  232.  
  233.   case CS_RGB:
  234.   case CS_YCbCr:
  235.   case CS_YIQ:
  236.     if (cinfo->num_components != 3)
  237.       ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
  238.     break;
  239.  
  240.   case CS_CMYK:
  241.     if (cinfo->num_components != 4)
  242.       ERREXIT(cinfo->emethods, "Bogus JPEG colorspace");
  243.     break;
  244.  
  245.   default:
  246.     ERREXIT(cinfo->emethods, "Unsupported JPEG colorspace");
  247.     break;
  248.   }
  249.  
  250.   /* Set color_out_comps and conversion method based on requested space */
  251.   switch (cinfo->out_color_space) {
  252.   case CS_GRAYSCALE:
  253.     cinfo->color_out_comps = 1;
  254.     if (cinfo->jpeg_color_space == CS_GRAYSCALE ||
  255.     cinfo->jpeg_color_space == CS_YCbCr ||
  256.     cinfo->jpeg_color_space == CS_YIQ) {
  257.       cinfo->methods->color_convert = grayscale_convert;
  258.       cinfo->methods->colorout_init = null_init;
  259.       cinfo->methods->colorout_term = null_term;
  260.     } else
  261.       ERREXIT(cinfo->emethods, "Unsupported color conversion request");
  262.     break;
  263.  
  264.   case CS_RGB:
  265.     cinfo->color_out_comps